﻿Imports System.Threading

Public Module DCM_Function
    Public Function Val(ByVal Expression As Char) As Integer
        Dim num2 As Integer = AscW(Expression)
        If ((num2 >= &H31) AndAlso (num2 <= &H39)) Then
            Return (num2 - &H30)
        End If
        Return 0
    End Function
    Private Function HexOrOctValue(ByVal InputStr As String, ByVal i As Integer) As Double
        Dim num4 As Long
        Dim num5 As Integer
        Dim num2 As Integer = 0
        Dim length As Integer = InputStr.Length
        Dim ch As Char = InputStr.Chars(i)
        i += 1
        If ((ch <> "H"c) AndAlso (ch <> "h"c)) Then
            If ((ch <> "O"c) AndAlso (ch <> "o"c)) Then
                Return 0
            End If
            Do While ((i < length) AndAlso (num2 < &H16))
                ch = InputStr.Chars(i)
                i += 1
                Dim ch3 As Char = ch
                If ((((ch3 <> ChrW(9)) AndAlso (ch3 <> ChrW(10))) AndAlso ((ch3 <> ChrW(13)) AndAlso (ch3 <> " "c))) AndAlso (ch3 <> ChrW(12288))) Then
                    If (ch3 = "0"c) Then
                        If (num2 = 0) Then
                            Continue Do
                        End If
                        num5 = 0
                    Else
                        If ((ch3 < "1"c) OrElse (ch3 > "7"c)) Then
                            Exit Do
                        End If
                        num5 = Asc(ch) - Asc("0"c)
                    End If
                    If (num4 >= &H1000000000000000) Then
                        num4 = ((num4 And &HFFFFFFFFFFFFFFF) * 8)
                        num4 = (num4 Or &H1000000000000000)
                    Else
                        num4 = (num4 * 8)
                    End If
                    num4 = (num4 + num5)
                    num2 += 1
                End If
            Loop
        Else
            Do While ((i < length) AndAlso (num2 < &H11))
                ch = InputStr.Chars(i)
                i += 1
                Dim ch2 As Char = ch
                If ((((ch2 <> ChrW(9)) AndAlso (ch2 <> ChrW(10))) AndAlso ((ch2 <> ChrW(13)) AndAlso (ch2 <> " "c))) AndAlso (ch2 <> ChrW(12288))) Then
                    If (ch2 = "0"c) Then
                        If (num2 = 0) Then
                            Continue Do
                        End If
                        num5 = 0
                    ElseIf ((ch2 >= "1"c) AndAlso (ch2 <= "9"c)) Then
                        num5 = (Asc(ch) - Asc("0"c))
                    ElseIf ((ch2 >= "A"c) AndAlso (ch2 <= "F"c)) Then
                        num5 = (Asc(ch) - Asc("7"c))
                    Else
                        If ((ch2 < "a"c) OrElse (ch2 > "f"c)) Then
                            Exit Do
                        End If
                        num5 = (Asc(ch) - Asc("W"c))
                    End If
                    If ((num2 = 15) AndAlso (num4 > &H7FFFFFFFFFFFFFF)) Then
                        num4 = ((num4 And &H7FFFFFFFFFFFFFF) * &H10)
                        num4 = num4 Or &H8000000000000000
                    Else
                        num4 = (num4 * &H10)
                    End If
                    num4 = (num4 + num5)
                    num2 += 1
                End If
            Loop
            If (num2 = &H10) Then
                i += 1
                If (i < length) Then
                    ch = InputStr.Chars(i)
                End If
            End If
            If (num2 <= 8) Then
                If ((num2 > 4) OrElse (ch = "&"c)) Then
                    If (num4 > &H7FFFFFFF) Then
                        num4 = (-2147483648 + (num4 And &H7FFFFFFF))
                    End If
                ElseIf (((num2 > 2) OrElse (ch = "%"c)) AndAlso (num4 > &H7FFF)) Then
                    num4 = (-32768 + (num4 And &H7FFF))
                End If
            End If
            Select Case ch
                Case "%"c
                    num4 = CShort(num4)
                    Exit Select
                Case "&"c
                    num4 = CInt(num4)
                    Exit Select
            End Select
            Return CDbl(num4)
        End If
        If (num2 = &H16) Then
            i += 1
            If (i < length) Then
                ch = InputStr.Chars(i)
            End If
        End If
        If (num4 <= &H100000000) Then
            If ((num4 > &HFFFF) OrElse (ch = "&"c)) Then
                If (num4 > &H7FFFFFFF) Then
                    num4 = (-2147483648 + (num4 And &H7FFFFFFF))
                End If
            ElseIf (((num4 > &HFF) OrElse (ch = "%"c)) AndAlso (num4 > &H7FFF)) Then
                num4 = (-32768 + (num4 And &H7FFF))
            End If
        End If
        Select Case ch
            Case "%"c
                num4 = CShort(num4)
                Exit Select
            Case "&"c
                num4 = CInt(num4)
                Exit Select
        End Select
        Return CDbl(num4)
    End Function
    Public Function Val(ByVal InputStr As String) As Double
        Dim length As Integer
        Dim num4 As Integer
        Dim num5 As Integer
        Dim num6 As Integer
        Dim num7 As Double
        If (InputStr Is Nothing) Then
            length = 0
        Else
            length = InputStr.Length
        End If
        Dim num2 As Integer = 0
        Do While (num2 < length)
            Select Case InputStr.Chars(num2)
                Case ChrW(9), ChrW(10), ChrW(13), " "c, ChrW(12288)
                    Exit Select
                Case ChrW(11), ChrW(12)
                    GoTo Label_004C
                Case Else
                    GoTo Label_004C
            End Select
            num2 += 1
        Loop
Label_004C:
        If (num2 >= length) Then
            Return 0
        End If
        Dim ch As Char = InputStr.Chars(num2)
        If (ch = "&"c) Then
            Return HexOrOctValue(InputStr, (num2 + 1))
        End If
        Dim flag As Boolean = False
        Dim flag2 As Boolean = False
        Dim flag3 As Boolean = False
        Dim y As Double = 0
        ch = InputStr.Chars(num2)
        Select Case ch
            Case "-"c
                flag3 = True
                num2 += 1
                Exit Select
            Case "+"c
                num2 += 1
                Exit Select
        End Select
        Do While (num2 < length)
            ch = InputStr.Chars(num2)
            Dim ch2 As Char = ch
            If (((ch2 = ChrW(9)) OrElse (ch2 = ChrW(10))) OrElse (((ch2 = ChrW(13)) OrElse (ch2 = " "c)) OrElse (ch2 = ChrW(12288)))) Then
                num2 += 1
            Else
                If (ch2 = "0"c) Then
                    If ((num4 <> 0) OrElse flag) Then
                        num7 = (((num7 * 10) + CDbl(Asc(ch))) - 48)
                        num2 += 1
                        num4 += 1
                    Else
                        num2 += 1
                    End If
                    Continue Do
                End If
                If ((ch2 >= "1"c) AndAlso (ch2 <= "9"c)) Then
                    num7 = (((num7 * 10) + CDbl(Asc(ch))) - 48)
                    num2 += 1
                    num4 += 1
                Else
                    If (ch2 = "."c) Then
                        num2 += 1
                        If flag Then
                            Exit Do
                        End If
                        flag = True
                        num6 = num4
                        Continue Do
                    End If
                    If (((ch2 = "e"c) OrElse (ch2 = "E"c)) OrElse ((ch2 = "d"c) OrElse (ch2 = "D"c))) Then
                        flag2 = True
                        num2 += 1
                    End If
                    Exit Do
                End If
            End If
        Loop
        If flag Then
            num5 = (num4 - num6)
        End If
        If Not flag2 Then
            If (flag AndAlso (num5 <> 0)) Then
                num7 = (num7 / Math.Pow(10, CDbl(num5)))
            End If
        Else
            Dim flag4 As Boolean = False
            Dim flag5 As Boolean = False
            Do While (num2 < length)
                ch = InputStr.Chars(num2)
                Dim ch3 As Char = ch
                If (((ch3 = ChrW(9)) OrElse (ch3 = ChrW(10))) OrElse (((ch3 = ChrW(13)) OrElse (ch3 = " "c)) OrElse (ch3 = ChrW(12288)))) Then
                    num2 += 1
                ElseIf ((ch3 >= "0"c) AndAlso (ch3 <= "9"c)) Then
                    y = ((y * 10) + CDbl((Asc(ch))) - 48)
                    num2 += 1
                Else
                    If (ch3 = "+"c) Then
                        If flag4 Then
                            Exit Do
                        End If
                        flag4 = True
                        num2 += 1
                        Continue Do
                    End If
                    If ((ch3 <> "-"c) OrElse flag4) Then
                        Exit Do
                    End If
                    flag4 = True
                    flag5 = True
                    num2 += 1
                End If
            Loop
            If flag5 Then
                y = (y + num5)
                num7 = (num7 * Math.Pow(10, -y))
            Else
                y = (y - num5)
                num7 = (num7 * Math.Pow(10, y))
            End If
        End If
        If Double.IsInfinity(num7) Then
            Throw New Exception(6)
        End If
        If flag3 Then
            num7 = -num7
        End If
        Select Case ch
            Case "&"c
                If (num5 > 0) Then
                    Throw New Exception(13)
                End If
                Return CInt(Math.Round(num7))
            Case "@"c
                Return Convert.ToDouble(New Decimal(num7))
            Case "!"c
                Return CSng(num7)
            Case "%"c
                If (num5 > 0) Then
                    Throw New Exception(13)
                End If
                Return CShort(Math.Round(num7))
        End Select
        Return num7
    End Function


    Public Function Val(ByVal Expression As Object) As Double
        If IsNothing(Expression) Then Return 0
        If IsDBNull(Expression) Then Return 0
        Dim str As String = Expression.ToString
        Return Val(str)
    End Function




End Module
